# f = open("39.元类.py", "r")
#
# f.close()
import random
import time


# with open("39.元类.py", "r") as f:
#     pass


# class FileHandler:
#     def __init__(self, filename, mode="r"):
#         self.filename = filename
#         self.mode = mode
#         self.file = None
#
#     def __enter__(self):
#         print(f"进入")
#         self.file = open(self.filename, self.mode, encoding="utf8")
#         return self.file
#
#     def __exit__(self, exc_type, exc_val, exc_tb):
#         print(f"离开")
#         self.file.close()
#
#
# # fh = FileHandler()
#
# with FileHandler("39.元类.py", "r") as fh:
#     print(fh.read())
#
#


class TimeCount:
    def __enter__(self):
        self.start = time.time()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.end = time.time()
        print(f"时间开销:{self.end - self.start}")


l = [random.randint(1, 10000) for i in range(1000)]
l2 = l.copy()
l3 = l.copy()

# start = time.time()
# l.sort()
# end = time.time()
# print(end-start)


with TimeCount() as tc:
    l.sort()

with TimeCount() as tc:
    sorted(l2)

with TimeCount() as tc:
    for i in range(len(l3) - 1):
        min_index = i
        for j in range(i + 1, len(l3)):
            if l[min_index] > l[j]:
                min_index = j
        if min_index != i:
            l[min_index], l[i] = l[i], l[min_index]
